#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "MatrixKey.h"

void main(){
	
	unsigned char KeyNum = 0;
	unsigned int Password = 0;
	unsigned char Count = 0;
	LCD_Init();
	LCD_ShowString(1, 1, "Password:");
	while(1){
		KeyNum = MatrixKey();
		if(KeyNum){
			// 如果是s1~s10按键按下，则输入密码
			if(KeyNum <= 10){
				if(Count < 4){
					// 密码左移一位
					Password *= 10;
					// 获取一位密码
					Password += KeyNum%10;
					++Count;
				}
				// 更新显示
				LCD_ShowNum(2, 1, Password, 4);
			} else if(KeyNum == 11){
				// s11按键为确认键
				LCD_ShowString(1, 11, "     ");
				if(Password == 2345){
					LCD_ShowString(1, 14, "OK");
				} else{
					LCD_ShowString(1, 11, "Error");
				}
				Password = 0;
				Count = 0;
				LCD_ShowNum(2, 1, Password, 4);
			} else if(KeyNum == 12){
				Password = 0;
				Count = 0;
				LCD_ShowNum(2, 1, Password, 4);
			}
		}
	}
}